<template>
  <j-modal :title="title" :width="600" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 20 }" :wrapper-col="{ span: 24 }">
      <a-form-model-item label="访谈分类" prop="interviewClassificationId">
        <a-tree-select style="width:100%" treeDefaultExpandAll :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
          :treeData="treeData" v-model="form.interviewClassificationId" @change="onChange" placeholder="请选择">
        </a-tree-select>
      </a-form-model-item>
      <a-form-model-item label="访谈主题" prop="interviewTheme">
        <a-input v-model="form.interviewTheme" placeholder="请输入" />
      </a-form-model-item>
      <a-form-model-item label="访谈大纲" class="beforeRequiredSign">
        <InterviewItem ref="interviewItemId" :isHaveAnswer="false" />
      </a-form-model-item>
      <a-form-model-item label="访谈资料">
        <j-upload v-model="form.applyFile" :number="1"></j-upload>
        <p>支持扩展名：.rar .zip .doc .docx .pdf .jpg...</p>
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as interviewApi from '@/api/sjsk/help/interview.js'
import InterviewItem from '@/views/sjsk/helpManage/modules/InterviewItem'
export default {
  props: ['title'],
  components: {
    InterviewItem
  },
  data() {
    return {
      visible: false,
      form: {},
      rules: {
        interviewClassificationId: [{ required: true, message: '请选择' }],
        interviewTheme: [{ required: true, message: '请输入' }],
      },
      treeData: [],
      loading: false,
      isAdd: true
    }
  },
  async created() {
  },
  methods: {
    show(id) {
      this.visible = true
      this.form = {}
      this.getTypeData()
      if (id) {
        this.isAdd = false
        this.form.id = id
        this.getDetail(id)
      } else {
        this.isAdd = true
      }
    },
    getTypeData() {
      interviewApi.classificationTree().then((res) => {
        if (res.success) {
          this.treeData = res.result ? res.result : {}
          this.generateList(this.treeData)
        } else {
          this.$message.success(res.message)
        }
      })
        .catch((e) => {
          this.$message.warning(res.message)
        })
    },
    generateList(data) {
      for (let i = 0; i < data.length; i++) {
        const node = data[i];
        // 不是叶子节点都不能选
        if (node.children && node.children !== null && node.children.length > 0) {
          // if (node.sortNum == 1) {
          //   node.disabled = true;
          // }
        } else {
          if (node.sortNum == 1) { //已经添加过的值灰，不可选择
            node.disabled = true;
          }
        }
        if (node.children) {
          this.generateList(node.children);
        }
      }
    },
    getDetail(id) {
      interviewApi.InterviewTemplateDetail(id).then(res => {
        if (res.success) {
          if (res.result != null) {
            this.$set(this.form, "interviewClassificationId", res.result.interviewClassificationId)
            this.$set(this.form, "interviewClassification", res.result.interviewClassification)
            this.$set(this.form, "interviewTheme", res.result.interviewTheme)
            this.$set(this.form, "applyFile", res.result.applyFile)
            let obj = {
              base: res.result.base,
              analyze: res.result.analyze,
              expand: res.result.expand
            }
            this.$refs.interviewItemId.setData(obj)
          }
        }
      })
    },
    handleCancel() {
      this.visible = false
    },
    onChange(value, label) {
      // console.log(value, label)
      this.form.interviewClassification = label[0]
      // console.log(this.form.interviewClassification)
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let itemData = this.$refs.interviewItemId.getData() //获取访谈访谈大纲
          if (itemData != undefined) {
            this.form = Object.assign(this.form, itemData)
            this.loading = true
            let api = ""
            if (this.isAdd) {
              api = interviewApi.addInterviewTemplate(this.form)
            } else {
              api = interviewApi.updateInterviewTemplate(this.form)
            }
            api.then(res => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok');
                this.visible = false
                this.loading = false
              } else {
                this.loading = false
                this.$message.warn(res.message)
              }
            })
              .catch(e => {
                this.loading = false
                this.$message.warn('请求失败！')
              })
          }
        }
      })
    }
  },
}
</script>
<style lang="less" scoped>
/deep/.ant-col-20 {
  text-align: left;
}
</style>
<style>
.beforeRequiredSign label::before {
  display: inline-block;
  margin-right: 4px;
  color: #f5222d;
  font-size: 14px;
  font-family: SimSun, sans-serif;
  line-height: 1;
  content: '*';
}
</style>
